﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Xml.Serialization;
using System.Xml;
using Chinaer.Model.Orm;

namespace Chinaer.Core.Helper
{
    public class EntityManagerFactoryHelper
    {
        public static IList QueryForList(string statementName, IList<SqlCondition> parameterObject, System.Type objectType)
        {
            DateTime startTime = DateTime.Now;
            IList reValue = null;
            object tmpObj = null;


            string sqlStatement = string.Empty;
            if (tmpObj == null)
            {
                try
                {
                    sqlStatement = ChinaerInfoManager.Instance.GetSqlInOrmStatementsByStatementName(statementName);
                    if (parameterObject != null)
                    {
                        foreach (var item in parameterObject)
                        {
                            string currKey = item.Key;
                            string currValue = item.Value;
                            sqlStatement = sqlStatement.Replace(":" + currKey, currValue);
                        }                       
                    }

                    DataSet dataSet = SqlHelper.QueryForObject(sqlStatement, objectType.Name);

                    //   this.LogStatement(statementName, objectType, startTime, sqlStatement);
                    startTime = DateTime.Now;

                    if (dataSet != null)
                    {
                        if (dataSet.Tables.Count > 0)
                        {
                            if (dataSet.Tables[0].Rows.Count > 0)
                            {

                                Type build = typeof(NewDataSet<>);
                                Type x = build.MakeGenericType(objectType);

                                dataSet.DataSetName = "Items";
                                XmlDataDocument xmlDoc = new XmlDataDocument();
                                xmlDoc.LoadXml(dataSet.GetXml());
                                XmlNode node = xmlDoc.SelectSingleNode(@".");
                                XmlDataDocument xmlDoc1 = new XmlDataDocument();
                                xmlDoc1.LoadXml("<NewDataSetOf" + objectType.Name + ">" + node.InnerXml + "</NewDataSetOf" + objectType.Name + ">");
                                XmlNode node1 = xmlDoc1.SelectSingleNode(@".");

                                XmlSerializer serializer = new XmlSerializer(x);

                                object newDataSet = serializer.Deserialize(new XmlNodeReader(node1));


                                Array arr = x.GetProperty("Items").GetValue(newDataSet, null) as Array;
                                reValue = new ArrayList(arr);
                            }
                        }

                        //   this.LogDeserializeTime(statementName, objectType, startTime);
                    }

                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            else
            {
                reValue = (IList)tmpObj;
            }
            return reValue;
        }
        public static object QueryForObject(string statementName, IList<SqlCondition> parameterObject, System.Type objectType)
        {
            DateTime startTime = DateTime.Now;
            object reValue = null;

            string sqlStatement = string.Empty;

            try
            {
                sqlStatement = ChinaerInfoManager.Instance.GetSqlInOrmStatementsByStatementName(statementName);
                if (parameterObject != null)
                {
                   // IDictionary parameterObjects = (IDictionary)parameterObject;
                    foreach (var item in parameterObject)
                    {
                        string currKey = item.Key;
                        string currValue = item.Value;
                        sqlStatement = sqlStatement.Replace(":" + currKey, currValue);                        
                    }                   
                }

                System.Data.DataSet dataSet = SqlHelper.QueryForObject(sqlStatement, objectType.Name);

                //   this.LogStatement(statementName, objectType, startTime, sqlStatement);
                startTime = DateTime.Now;

                if (dataSet != null)
                {
                    if (dataSet.Tables.Count > 0)
                    {
                        if (dataSet.Tables[0].Rows.Count > 0)
                        {


                            dataSet.DataSetName = "Items";
                            Type build = typeof(NewDataSet<>);
                            Type x = build.MakeGenericType(objectType);


                            XmlDataDocument xmlDoc = new XmlDataDocument();
                            xmlDoc.LoadXml(dataSet.GetXml());
                            XmlNode node = xmlDoc.SelectSingleNode(@".");
                            XmlDataDocument xmlDoc1 = new XmlDataDocument();
                            xmlDoc1.LoadXml("<NewDataSetOf" + objectType.Name + ">" + node.InnerXml + "</NewDataSetOf" + objectType.Name + ">");
                            XmlNode node1 = xmlDoc1.SelectSingleNode(@".");

                            XmlSerializer serializer = new XmlSerializer(x);

                            object newDataSet = serializer.Deserialize(new XmlNodeReader(node1));

                            Array arr = x.GetProperty("Items").GetValue(newDataSet, null) as Array;
                            reValue = (new ArrayList(arr))[0];
                        }
                    }

                    //  this.LogDeserializeTime(statementName, objectType, startTime);
                }


            }
            catch (Exception ex)
            {
                //LogHelper.WriteLog("EntityManagerFactory", "QueryForObject::ERROR::" + statementName, ex.Message);
                //LogHelper.Error<EntityManagerFactory>("QueryForObject:" + statementName, ex);

                throw ex;
            }
            return reValue;
        }

    }
    [Serializable]
    public class NewDataSet<T>
    {
        private T[] items;
        public T[] Items
        {
            get
            {
                return this.items;
            }
            set
            {
                this.items = value;
            }
        }

    }
}
